const app = getApp();

Page({
    data: {
        type: 1,
        subject: 1,
        num: 1,
        checkedId: null,
        goods: [],
        goodsTitle: '',
        intros: [
            '',
            '错题汇总卷每周推送一次，请您选择您所需的科目和次数。',
            '错题拓展卷是根据学生日常作业中的错题总结出的相同知识点的题目，每两周推送一次。',
            '错题一对一是一里学堂推出的线下辅助教育模式，上课前老师会拿到学生的错题资料，有针对性的对学生进行一对一辅导。您下单后会有专门的老师帮您预约上课时间和上课地点。',
            '学期整训只针对使用一里学堂超过2个月的学生，学堂老师首先对本学期所学知识做系统性的讲解梳理，然后根据学生的日常表现有针对性的进行辅导。',
            '中考名师讲解目前只针对初三的学生，一里学堂会根据学生一学期的表现为学生匹配相应的名师，补足知识漏洞，建立系统的知识体系，分析中考考点，帮助学生在中考中取得优异的成绩。',
        ],
        inputInfo: 0,

        orderEmail: '',
        orderPhone: '',
        orderAddress: '',
    },
    onLoad: function(optioins) {
        var self = this;
        this.setData({
            type: optioins.type || '1',
        });
        this.getGoods();
        // console.log(this.data.type);
    },
    selectSubject: function(e) {
        this.setData({
            subject: e.currentTarget.dataset.value,
        });
        this.getGoods();
        // console.log(this.data.subject)
    },
    getGoods: function() {
        var self = this;
        app.showLoading();
        wx.request({
            url: app.api.goodsListUrl + self.data.type + '/' + self.data.subject + '?time=' + Date.parse(new Date()),
            data: {},
            header: { 'Content-Type': 'application/json' },
            method: 'POST',
            success: function(rs) {
                var list = rs.data.data;
                self.setData({
                    checkedId: list[0]['goodId'],
                    goods: list,
                    goodsTitle: list[0]['goodName'].split('-')[0],
                });
                app.hideLoading();
                // console.log(self.data.checkedId)
            }
        });
    },
    setCheckedId: function(e) {
        this.setData({
            checkedId: e.currentTarget.dataset.value,
            // num: 0,
        });
        console.log(this.data.checkedId)
    },
    setNum: function(e) {
        var _num = (this.data.num + parseInt(e.currentTarget.dataset.value)),
            _num = _num > 0 ? _num : 0;
        this.setData({
            num: _num,
        });
        console.log(this.data.num)
    },
    goInputInfo: function(e) {
        this.setData({
            inputInfo: 1,
        });
    },    
    bindInputChange: function(e) {
        var _json = this.data;
        _json[e.currentTarget.dataset.valuename] = e.detail.value;
        this.setData(_json)
        // console.log(this.data)
    },
    submitOrder: function(e) {
        // console.log(e)
        if(app.status.isLoading){
            console.log('不能重复提交');
            return;
        }
        app.showLoading(1);
        var that = this;
        var warn = ""; //弹框时提示的内容
        var flag = true; //判断信息输入是否完整
        that.setData({
            orderEmail: that.data.orderEmail.replace(/\s+/,''),
        });
        if (that.data.orderEmail == "") {
            warn = "请填写您的收件邮箱！";
        } else if (!(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/.test(that.data.orderEmail))) {
            warn = "收件邮箱格式不正确！";
        } else if (that.data.orderPhone == "") {
            warn = "请填写您的手机号码！";
        } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(that.data.orderPhone))) {
            warn = "手机号码格式不正确";
        } else if (that.data.orderAddress == "") {
            warn = "请填写您的收件地址！";
        } else {
            flag = false;
            wx.request({
                url: app.api.reserveOrder,
                data: {
                    // 'id':123,
                    'goodsId': that.data.checkedId,
                    'subjectId': that.data.subject,
                    'orderPhone': that.data.orderPhone,
                    'orderEmail': that.data.orderEmail,
                    'orderAddress': that.data.orderAddress,
                    'orderCount': that.data.num,
                    'openId': app.globalData.openId
                },
                header: { 'Content-Type': 'application/json' },
                method: 'POST',
                success: function(rs) {
                    if (rs.data.status == '0') {
                        wx.navigateTo({
                          url: app.url.pay+'?orderInfo='+JSON.stringify(rs.data.data)+'&goodsName='+that.data.goodsTitle,
                        });
                        app.hideLoading(1);
                        // wx.showModal({
                        //     title: '提示',
                        //     content: '提交订单成功！',
                        //     success:function(){
                        //         wx.navigateTo({
                        //           url: app.globalData.index_url
                        //         })
                        //     }
                        // })
                    } else {
                        app.hideLoading(1);
                        wx.showModal({
                            title: '提示',
                            content: '提交订单失败！'
                        });
                    }
                }
            });
        }
        //如果信息填写不完整，弹出输入框
        if (flag == true) {
            app.hideLoading(1);
            wx.showModal({
                title: '提示',
                content: warn
            })
        }

    },
})